import { Injectable  } from '../../support'
import { get_plugin_data, get_plugin } from '@backend/includes/plugin'
import path from 'path'
import { BrowserView, BrowserWindow, app } from 'electron'
import GlobalBrowserViewManager, { GlobalBrowsViewsMng } from '@backend/utils/globalBrowserViewManager'
import Globals from '@backend/common/Globals'

// MyService.ts
@Injectable('SystemService')
export class SystemService {
  constructor() { /* do nothing */ console.log('init systemservice')  }

  public async getSysBars(is_menu: boolean ) {
    const list = await get_plugin_data({ is_menu: is_menu })
    return list
  }

  public async getSysBarSubs( pluginId: number){
    const plugin = await get_plugin(pluginId)
    return plugin.getSubsList()
  }

/*以下为老版本支持 */
  public reply111(): string {
    return 'Hello! system Renderer Process!'
  }

  public testMessage(): string{
    console.log('in testService ...')
    return 'this is a test Message in system'
  }

  getMenus( code: string): any[]{
    return code=='my-app' ? [
        { apptag: 'plugin.local.shiwu', title:'事务管理', icon:'table', desc: '管理您的个人事务，实时提醒', btns: [{title: '新建一个事务'},{title: '最近提醒'}] },
        { apptag: 'plugin.local.book', title:'笔记本', icon:'pen', desc: '我的文档编辑器', btns: [{title: '新建笔记'},{title: '我的笔记本'}] },
        { apptag: 'plugin.local.naotu', title:'脑图风暴', icon:'kanban', desc: '脑图一出，无事不通', btns: [{title: '新建一个脑图'},{title: '管理列表'}] },
        { apptag: 'plugin.local.kanban', title:'我的看板', icon:'kanban', desc: '管理您的个人事务，实时提醒', btns: [{title: '有更新...'},{title: '创建一个看板'},{title: '消息'}] },
        { apptag: 'plugin.local.mohe', title:'数据魔盒', icon:'layout-wtf', desc: '高效处理您的工作文件，图表预览', btns: [{title: '新建一个图表'},{title: '图形化处理您的excel'}] },
        { apptag: 'plugin.local.wenjuan', title:'在线问卷生产工具', icon:'graph-up-arrow', desc: '市场调研，问卷编辑，打分，模拟考试', btns: [{title: '新建一个问卷'},{title: '问卷报表'}] },
        { apptag: 'plugin.local.wendang', title:'文档生成工具', icon:'clipboard-data', desc: '文档生产工具，轻松导出pdf,word,html,图片等', btns: [{title: '新增文档'},{title: '我的文档列表'}] },
        //{ apptag: 'plugin.local.jianli', title:'简历制作', icon:'emoji-heart-eyes', desc: '轻松管理我的简历，方便导出pdf,word,html,图片等。超1w+公司', btns: [{title: '我的简历'},{title: '制作我的模板'}] },
        { apptag: 'plugin.local.daping', title:'大屏可视化', icon:'tv', desc: '大屏数据可视化，业绩战报，仪表排盘', btns: [{title: '新建一个报表'},{title: '图形化处理您的excel'}] },
        { apptag: 'plugin.local.shipin', title:'视频剪辑', icon:'camera-video', desc: '一个高效的视频自动剪辑成合成工具', btns: [{title: '新建一个剪辑'},{title: '新建一个视频资源'}] },
        { apptag: 'plugin.local.tupian', title:'图片制作', icon:'card-image', desc: '可视化对图片的整合处理，宣传图制作好帮手', btns: [{title: '新建事务'},{title: '新建事务'}] },
        { apptag: 'plugin.local.chatgpt', title:'chatGPT', icon:'chat-dots', desc: 'chatGPT聊天', btns: [] }
    ] : []
  }

  getMenusBars(): any[]{
    return [
        {
            icon: 'cup-hot-fill',
            code: 'my-app',
            title: '我的应用'
        },
        {
            icon: 'kanban-fill',
            code: 'my-kanban',
            title: '我的看板'
        },
        {
            icon: 'calendar-check-fill',
            code: 'my-date',
            title: '我的看板'
        },
        {
            icon: 'xbox',
            code: 'my-xbox',
            title: '我的看板'
        },
        {
            icon: 'x-diamond-fill',
            code: 'my-diamond',
            title: '我的看板'
        },
        {
            icon: 'vinyl',
            code: 'my-vinyl',
            title: '我的看板'
        },
        {
            icon: 'globe-asia-australia',
            code: 'my-asia',
            title: '我的看板'
        },
        {
            icon: 'gear-fill',
            code: 'my-fill',
            title: '我的看板'
        }
    ]
  }

  createBrowserView( p : any ): string{
    const viewInfo = GlobalBrowserViewManager.createView( p.apptag )
    const view = viewInfo.view
    GlobalBrowsViewsMng.browserWindow.setBrowserView(view)
    view.setBounds({ x: p.x, y: p.y, width: p.width, height: p.height })
    const pluginPath = Globals.getGlobal('plugin-path'); //path.resolve( path.dirname( app.getPath('exe') ), 'plugins', p.apptag )
    view.webContents.loadFile(path.resolve(pluginPath, p.apptag ,'index.html') )
    return viewInfo.id
  }

  browserViewDestory( id: string ){
    GlobalBrowserViewManager.removeBrowserView( id )
  }

  handleBrowserViewResize( p: any ){
    const id = p.id
    if(!id) return

    const view = GlobalBrowserViewManager.getBrowserView( id )
    if(view.view){
      view.view.setBounds({ x: p.x, y: p.y, width: p.width, height: p.height })
    }
  }

  browerViewActive( id: string, o: object = null ){
    const view = GlobalBrowserViewManager.getBrowserView( id )
    if(view && view.view){
      if( o ){

      }
      GlobalBrowsViewsMng.browserWindow.setBrowserView(view.view);
    //   view.view.setBounds({ x: p.x, y: p.y, width: p.width, height: p.height })
    }
  }

  pluginAppBootPath( apptag: string ){
    // 'file:///D:/Iori.Work/iori/Projects/cubex/src/plugins/home/index.html'
    return 'http://localhost:3000/main_window'
  }
/*以上为老版本支持 */
}